package org.gc.service.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gc.dao.base.BaseDao;
import org.gc.util.dozer.DozerBeanUtil;
import org.gc.webapp.util.page.BasePageSO;
import org.gc.webapp.util.page.PageList;
import org.springframework.beans.factory.annotation.Autowired;


public abstract class BaseServiceImpl<T, VO, PK extends Serializable> implements BaseService<T, VO, PK> {
    /**
     * Logger for this class
     */
    @SuppressWarnings("unused")
    private static final Log logger = LogFactory.getLog(BaseServiceImpl.class);

    private BaseDao<T, PK> baseDao;
    @SuppressWarnings("unchecked")
    private Class entityClass;
    @SuppressWarnings("unchecked")
    private Class entityVOClass;
    
    protected DozerBeanUtil dozerBeanUtil;
    
    /**
     * 请在这里给baseDao，modelClass和modelVOClass赋值
     * eg.
     *      UserServiceImpl.java:
     *              @PostConstruct
     *              public void afterConstruct(){
     *                  super.setBaseDao(userDao);
     *                  super.setModelClass(user.class);
     *                  super.setModelVOClass(userVO.class);
     *              }
     */
    @PostConstruct
    abstract public void afterConstruct();
    
    public void setBaseDao(BaseDao<T, PK> baseDao) {
        this.baseDao = baseDao;
    }

    public Class getEntityClass() {
        return entityClass;
    }

    public void setEntityClass(Class entityClass) {
        this.entityClass = entityClass;
    }

    public Class getEntityVOClass() {
        return entityVOClass;
    }

    public void setEntityVOClass(Class entityVOClass) {
        this.entityVOClass = entityVOClass;
    }

    @Autowired
    public void setDozerBeanUtil(DozerBeanUtil dozerBeanUtil) {
        this.dozerBeanUtil = dozerBeanUtil;
    }
    
    public boolean exists(PK id) {
        return baseDao.exists(id);
    }

    
    public List<VO> findByNamedQuery(String queryName, Map<String, Object> queryParams) {
        List<VO> result = new ArrayList<VO>();
        dozerBeanUtil.convert(baseDao.findByNamedQuery(queryName, queryParams), result);
        return result;
    }

    
    @SuppressWarnings("unchecked")
    public VO get(PK id) {
        VO result = (VO)dozerBeanUtil.convert(baseDao.get(id), entityVOClass);
        return result;
    }

    
    @SuppressWarnings("unchecked")
    public List<VO> getAll() {
        List<VO> result = new ArrayList<VO>();
        result = (List<VO>)dozerBeanUtil.convertList(baseDao.getAll(), entityVOClass);
        return result;
    }

    
    @SuppressWarnings("unchecked")
    public VO getBy(String fieldName, Object value) {
        VO result = (VO)dozerBeanUtil.convert(baseDao.getBy(fieldName, value), entityVOClass);
        return result;
    }

    
    public List<VO> getByIds(List<PK> ids) {
        List<VO> result = new ArrayList<VO>();
        dozerBeanUtil.convert(baseDao.getByIds(ids), result);
        return result;
    }

    
    public PageList<VO> getPageList(BasePageSO so) {
        PageList<VO> result = new PageList<VO>();
        result = dozerBeanUtil.convertPageList(baseDao.getPageList(so), entityVOClass);
        return result;
    }

    
    public void remove(PK id) {
        baseDao.remove(id);

    }

    
    @SuppressWarnings("unchecked")
    public VO save(VO object) {
        T t = (T)dozerBeanUtil.convert(object, entityClass);
        VO result = (VO)dozerBeanUtil.convert(baseDao.save(t), entityVOClass);
        return result;
    }

    @SuppressWarnings("unchecked")
    public List<VO> getListBy(String fieldName, Object value) {
        List<VO> result = new ArrayList<VO>();
        result = dozerBeanUtil.convertList(baseDao.getListBy(fieldName, value), entityVOClass);
        return result;
    }
    
}

